package com.acme.bankapp.client;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;

/**
 * Created by splash on 20.11.2014.
 */
public class BankClientTransport {
    private Socket socket;
    private DataOutputStream out;
    private DataInputStream in;

    public BankClientTransport() throws IOException {
        try {
            socket = new Socket("127.0.0.1", 6669);
            out = new DataOutputStream(socket.getOutputStream());
            in = new DataInputStream(socket.getInputStream());

            System.out.println("Session started. Please, enter command (addnew, info, exit):");
        } finally { }
    }

    public Socket getSocket() {
        return socket;
    }

    public ArrayList<String> request(String message) throws IOException {
        ArrayList<String> result = new ArrayList<String>();

        out.writeUTF(message);

        String line = in.readUTF();
        while (!line.equals("eof")) {
            result.add(line);
            line = in.readUTF();
        }

        return result;
    }
}
